//
//  C_NewSongViewController.m
//  音悦咖
//
//  Created by Best on 15/7/15.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "C_NewSongViewController.h"
#import "C_NewSongCollectionViewCell.h"


@interface C_NewSongViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *carefulDataArr;
@property (nonatomic, retain) NSMutableArray *latestDataArr;
@end

@implementation C_NewSongViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    self.rightButton.hidden = YES;
    self.titleLabel.text = @"新歌";
    backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    backgroundImage.image = [UIImage imageWithContentsOfFile:GETBUNDLE_PATH(@"blackgound", @"jpg")];
    [self.view addSubview:backgroundImage];
    backgroundImage.userInteractionEnabled = YES;
    [backgroundImage release];
    [self getdata];
}



#pragma marl getaData
- (void)getdata
{
    [AFNetworkHandler GETWithAFNByURL:self.NewSong_url completion:^(id result) {
        self.carefulDataArr = [C_SonglistModel arrayWithModelByArray:[result objectForKey:@"singles"]];
        self.latestDataArr = [C_SonglistModel arrayWithModelByArray:[result objectForKey:@"data"]];
        [self createCollection];

        [self createCircleColletionHeader];
        
        [self createMainHeader];
    }];
    
    
}


- (void)createCollection
{
#pragma mark mainCollection
    UICollectionViewFlowLayout *mainLayout = [[UICollectionViewFlowLayout alloc] init];
    mainLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 5);
    mainLayout.minimumLineSpacing = 1;

    mainCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(-SCREEN_WIDTH, 0, SCREEN_WIDTH * 2, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:mainLayout];
    mainCollection.backgroundColor = [UIColor clearColor];
    mainCollection.delegate = self;
    mainCollection.dataSource = self;
    mainCollection.tag = 2200;
    
    [mainCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [mainCollection registerClass:[C_SongListCollectionViewCell class] forCellWithReuseIdentifier:@"C_SongListCollectionViewCell"];
    [backgroundImage addSubview:mainCollection];
    [mainCollection release];

}

#pragma mark Collection header
- (void)createCircleColletionHeader
{
    collectionHeader = [[C_CarefullyChosenCollectionReusableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, 50)];
    collectionHeader.imageView.image = [[UIImage imageNamed:@"chosen"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    collectionHeader.backgroundColor = [UIColor clearColor];
    collectionHeader.titleLabel.text = @"精选歌曲";
    collectionHeader.titleLabel.textColor = WHITE;
    [mainCollection addSubview:collectionHeader];
    
#pragma mark 轮播collection
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    collection = [[UICollectionView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, collectionHeader.frame.size.height, SCREEN_WIDTH, SCREEN_WIDTH / 3) collectionViewLayout:layout];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 4, SCREEN_WIDTH / 4);
    layout.sectionInset = UIEdgeInsetsMake(10, SCREEN_WIDTH / 4 / 4, 10, SCREEN_WIDTH / 4 / 4);
    collection.backgroundColor = [UIColor clearColor];
    collection.delegate = self;
    collection.dataSource = self;
    collection.tag = 2100;
    collection.pagingEnabled = YES;
    layout.minimumLineSpacing = SCREEN_WIDTH / 16 ;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [mainCollection addSubview:collection];
#pragma mark CareCell重用池
    [collection registerClass:[C_NewSongCollectionViewCell class] forCellWithReuseIdentifier:@"C_NewSongCollectionViewCell"];
    [collection release];
    
}
#pragma mark mainHeader

- (void)createMainHeader
{
    mainheader = [[C_CarefullyChosenCollectionReusableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, collection.frame.origin.y + collection.frame.size.height, SCREEN_WIDTH, 50)];
    mainheader.imageView.image = [UIImage imageNamed:@"fashion"];
    mainheader.titleLabel.text = @"最新时尚";
    mainheader.titleLabel.textColor = WHITE;
    mainheader.titleLabel.font = [UIFont systemFontOfSize:20];
    mainheader.backgroundColor = [UIColor clearColor];
    [mainCollection addSubview:mainheader];
    
}
#pragma mark Header方法

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 2100:{
           
            return nil;
            
            break;
        }
        case 2200:{
            UICollectionReusableView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        
            return reusable;
            
            break;
        }
        default:
            break;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    switch (collectionView.tag) {
        case 2100: {
            return CGSizeMake(0, 0);
            break;
        }
        case 2200: {
            return CGSizeMake(SCREEN_WIDTH, mainheader.frame.size.height + collectionHeader.frame.size.height + SCREEN_WIDTH / 3);
            break;
        }
        default:
            break;
    }
    return CGSizeMake(0, 0);
}


#pragma mark collectionDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 2200:{
            C_NewSongPlayerViewController *player = [[C_NewSongPlayerViewController alloc] init];
            player.model = self.latestDataArr[indexPath.item];
            [self.navigationController pushViewController:player animated:YES];
            
            break;
        }
        case 2100:{
            C_NewSongPlayerViewController *player = [[C_NewSongPlayerViewController alloc] init];
            player.model = self.carefulDataArr[indexPath.item];
            [self.navigationController pushViewController:player animated:YES];
            
            break;
        }
        default:
            break;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (collectionView.tag) {
        case 2100:
            return self.carefulDataArr.count;
            break;
        case 2200:
            return self.latestDataArr.count;
            break;
        default:
            break;
    }
    return 0;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 2100: {
            C_NewSongCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_NewSongCollectionViewCell" forIndexPath:indexPath];
            cell.model = self.carefulDataArr[indexPath.item];
            cell.backgroundColor = [UIColor clearColor];
            cell.clipsToBounds = YES;
            cell.layer.cornerRadius = 5;
           // cell.mainImageView.backgroundColor = [UIColor orangeColor];
            return cell;
            break;
        }
        case 2200: {
            C_SongListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_SongListCollectionViewCell" forIndexPath:indexPath];
            cell.model = self.latestDataArr[indexPath.item];
            cell.clipsToBounds = YES;
          
            cell.backgroundColor = RGBA(212, 190, 140, 0.4);
            return cell;
            break;
        }
        default:
            break;
    }
   
    return nil;
}
//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
//{
//    switch (scrollView.tag) {
//        case 2100: {
//            CATransition *transition = [CATransition animation];
//            transition.type = @"cube";
//            transition.duration = 1;
//            transition.repeatCount = 1;
//            if (scrollView.contentOffset.x > 0) {
//                transition.subtype = kCAGravityTopRight;
//            } else {
//                transition.subtype = kCAGravityTopLeft;
//            }
//
//            [scrollView.layer addAnimation:transition forKey:@"tran"];
//            
//            break;
//        }
//            
//        default:
//            break;
//    }
//}


- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag == 2200) {
        
        CATransform3D rotation;
        rotation = CATransform3DMakeRotation((90.0 * M_PI)/ 180, 0.0, 1.0, 0.4);
        rotation.m34 = 1.0/ - 200;
        cell.layer.shadowColor = [[UIColor blackColor] CGColor];
        cell.layer.shadowOffset = CGSizeMake(10, 10);
        cell.alpha = 0;
        cell.layer.transform = rotation;
        cell.layer.anchorPoint = CGPointMake(0, 0.5);
        
        [UIView beginAnimations:@"rotation" context:nil];
        [UIView setAnimationDuration:0.8];
        cell.layer.transform = CATransform3DIdentity;
        cell.alpha = 1;
        cell.layer.shadowOffset = CGSizeMake(0, 0);
        [UIView commitAnimations];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
